package GUI.Gestion;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import net.miginfocom.swing.MigLayout;

import javax.swing.JComboBox;

import DataBase.DAO;
import Modelo.Rol;
import Modelo.Usuario;

public class dialog_Productos extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTable tabla;
	private boolean editable;
	private JComboBox comboBox;
	private Connection BD;
	private DAO op;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the dialog.
	 * 
	 * @wbp.parser.constructor
	 */
	public dialog_Productos(final JTable table, boolean edit,Connection c) {
		this.editable = edit;
		this.tabla = table;
		this.BD=c;
		op=new DAO();
		this.inicilize();

	}

	public dialog_Productos(final JTable table, int fila, boolean edit,Connection c) {
		this.editable = edit;
		this.tabla = table;
		this.BD=c;
		op=new DAO();
		this.inicilize();
		dispose();

	}

	private void inicilize() {

		setBounds(100, 100, 193, 210);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[46px,center][95px,grow]",
				"[][][][][]"));
		{
			JLabel lblId = new JLabel("ID");
			lblId.setHorizontalTextPosition(SwingConstants.CENTER);
			lblId.setHorizontalAlignment(SwingConstants.CENTER);
			contentPanel.add(lblId, "cell 0 0,alignx center,aligny center");
		}
		{
			textField_1 = new JTextField();
			contentPanel.add(textField_1,
					"cell 1 0,alignx center,aligny center");
			textField_1.setColumns(10);
		}
		{
			JLabel lblNombre = new JLabel("Nombre");
			contentPanel.add(lblNombre, "cell 0 1,alignx center");
		}
		{
			textField = new JTextField();
			contentPanel.add(textField, "cell 1 1,alignx center,aligny center");
			textField.setColumns(10);
		}
		{
			JLabel lblPuesto = new JLabel("Marca");
			contentPanel.add(lblPuesto, "cell 0 2,alignx center");
		}
		{
			textField_2 = new JTextField();
			contentPanel.add(textField_2,
					"cell 1 2,alignx center,aligny center");
			textField_2.setColumns(10);
		}
		{
			JLabel lblCategoria = new JLabel("Categoria");
			contentPanel.add(lblCategoria, "cell 0 3,alignx trailing");
		}
		{
			//JComboBox comboBox = new JComboBox();
			comboBox = new JComboBox();
			ArrayList al = new ArrayList<Usuario>();

		
			for (int i = 0; i < al.size(); i++) {

				Usuario user = (Usuario) al.get(i);
				comboBox.addItem(user.getNombre());
			}

			contentPanel.add(comboBox, "cell 1 3,growx");
		}
		{
			JLabel lblMarca = new JLabel("Precio");
			contentPanel.add(lblMarca, "cell 0 4,alignx center");
		}
		{
			textField_4 = new JTextField();
			contentPanel.add(textField_4,
					"cell 1 4,alignx center,aligny center");
			textField_4.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {

						if (!editable) {
							Tabla modelo = new Tabla();
							modelo = (Tabla) tabla.getModel();

							String[] aux = new String[5];
							aux[0] = textField_1.getText();
							aux[1] = textField.getText();
							aux[2] = textField_2.getText();
							aux[3] = "alimento";//comboBox.getSelectedItem().toString();
							aux[4] = textField_4.getText();
							op.insert("articulo", "id_articulo,nombre,marca,categoria,precio,stock,tienda", aux[0]+",'"+aux[1]+"','"+aux[2]+"','alimento',"+aux[4]+",0,001", BD);
							modelo.addRow(aux);
							tabla.setModel(modelo);
						} else {

							tabla.setValueAt(textField_1.getText(),
									tabla.getSelectedRow(), 0);
							tabla.setValueAt(textField.getText(),
									tabla.getSelectedRow(), 1);
							tabla.setValueAt(textField_2.getText(),
									tabla.getSelectedRow(), 2);
							tabla.setValueAt(comboBox.getSelectedItem()
									.toString(), tabla.getSelectedRow(), 3);
							tabla.setValueAt(textField_4.getText(),
									tabla.getSelectedRow(), 4);
							

						}
						dispose();

					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
